package cn.lds.j367.dsbank.service.impl;

import cn.lds.j367.dsbank.common.dto.ResultDTO;
import cn.lds.j367.dsbank.entity.Employee;
import cn.lds.j367.dsbank.entity.Role;
import cn.lds.j367.dsbank.mapper.EmpMapper;
import cn.lds.j367.dsbank.mapper.RoleMapper;
import cn.lds.j367.dsbank.service.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

@Service
public class EmpServiceimpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private BCryptPasswordEncoder bpe;

    private static final Logger log = LoggerFactory.getLogger(EmpServiceimpl.class);


    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public ResultDTO<List<Employee>> selectEmployeeByEmp(Employee employee) {

        List<Employee> employees = empMapper.selectEmployeeByEmp(null);

        if (CollectionUtils.isEmpty(employees)){
           return ResultDTO.buildError();
        }

        return ResultDTO.buildSuccess(employees);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public ResultDTO<Employee> selectEmpById(Integer empId) {

        Employee employee = empMapper.selectEmpById(empId);

        List<Role> roles = roleMapper.selectRoleByEmpId(empId);

        employee.setRoleList(roles);


        if (Objects.isNull(empId)){
            return ResultDTO.buildError();
        }

        return ResultDTO.buildSuccess(employee);
    }

    @Override
    public ResultDTO<Employee> loginEmp(Employee employee) {
        return null;
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResultDTO addEmp(Employee employee,Integer roles[]) {


        //给密码加密
        employee.setEmpPassword(bpe.encode(employee.getEmpPassword()));

        log.info("加密后用户：{}" ,employee);

        Integer integer = empMapper.addEmp(employee);

        if (integer <= 0){

            return ResultDTO.buildSuccess();
        }
        int empId = employee.getEmpId();
        log.info("员工Id:{}",empId);

        Stream.of(roles).forEach(r->{
            Integer addrole = empMapper.addEmpAndRole(empId, r);
        });

        return ResultDTO.buildSuccess();
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResultDTO modifyEmp(Employee employee) {
        return null;
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResultDTO deleteEmp(Integer empId) {
        return null;
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        Employee employee = empMapper.selectEmployeeByEmpUsername(s);


        return employee;
    }
}
